﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction {                              
    private bool running = false;
    private int round = 1;                                                  
    private int trial = 0;            
    private int time_counter = 0;
    public FlyActionManager action_manager;
    public DiskFactory factory;
    public UserGUI gui;
    public ScoreRecorder recorder;
    
    public void LoadResources() {
        factory.GetDisk(round);
        factory.FreeDisk();
    }
    void Start () {
        SSDirector director = SSDirector.GetInstance();     
        director.CurrentScenceController = this;
        action_manager = gameObject.AddComponent<FlyActionManager>() as FlyActionManager;
        factory = Singleton<DiskFactory>.Instance;
        gui = gameObject.AddComponent<UserGUI>() as UserGUI;
        recorder = Singleton<ScoreRecorder>.Instance;
    }
    // 只要游戏还在继续，就发射飞碟
	void Update () {
        if(running) {
            time_counter++;
            // Update中追踪鼠标左键点击位置，执行Hit()
            if (Input.GetButtonDown("Fire1")) {
                Vector3 pos = Input.mousePosition;
                Hit(pos);
            }
            // 根据round大小，发射飞碟的频率会越来越高
            if (time_counter >= 200-50*round) {
                time_counter = 0;
                SendDisk((int)(Random.Range(1, round+1)));
                trial += 1;    
                // trial每10个，进入下个round 
                if (trial == 10) 
                {
                    round += 1;
                    trial = 0;
                    // round=4的时候，游戏停止，结束
                    if(round == 4)
                        running = false;
                }
            }
            factory.FreeDisk();
        }
    }

    //发射飞碟： 需要生成飞碟，给飞碟初位置，给飞碟一个飞行的动作
    private void SendDisk(int type) {
        type = (type>3)? 3:type;

        GameObject disk = factory.GetDisk(type);

        float ran_x = Random.Range(-1f, 1f) < 0 ? -1 : 1;
        float ran_y = Random.Range(1f*type, 5f*type);
        float power = Random.Range(5f*type, 24f*type);
        float angle = Random.Range(25f/type,30f/type);

        disk.transform.position = new Vector3(ran_x*16f, ran_y, 0);
        if(gui.getMode()==0)
            action_manager.DiskFly(disk, angle, power);
        else    action_manager.PhyDiskFly(disk,power);
    }
    
    // 射击，对鼠标左键点击的位置实现射击飞碟
    public void Hit(Vector3 pos) {
        Ray ray = Camera.main.ScreenPointToRay(pos);
        RaycastHit[] hits;
        hits = Physics.RaycastAll(ray);
        // 遍历 射线上所有记录到的飞碟
        for (int i = 0; i < hits.Length; i++) {
            RaycastHit hit = hits[i];
            // 不是null说明击中了，就加分
            if (hit.collider.gameObject.GetComponent<Disk>() != null) {
                recorder.Record(hit.collider.gameObject);
                hit.collider.gameObject.transform.position = new Vector3(0, -10, 0);
            }
        }
    }
    //重开游戏
    public void Remake() {
        running = true;
        round = 1;
        trial = 1;
        recorder.Reset();
        factory.Reset();
    }

    public void GameOver() {
        running = false;
    }

    public float GetScore() {
        return recorder.GetScore();
    }
    public int GetRound() {
        return round;
    }
}
